#include "md5.c"

/* top-level md5sum function by Andrea Aloi */
string md5sum(FILE* fp) {
    md5_state_t state;
	md5_byte_t digest[16];
    //Calculate file's size, in bytes
    int length = filesize(fp);
    //Read file in buffer
    string buffer = (string)malloc(length*sizeof(string));
    fread(buffer, 1, length, fp);
	string hex_output = (string)malloc(33*sizeof(string));
    int di;
	md5_init(&state);
	md5_append(&state, (const md5_byte_t *)buffer, length);
	md5_finish(&state, digest);
	for (di = 0; di < 16; ++di) sprintf(hex_output + di * 2, "%02x", digest[di]);
	return hex_output;
}
